<template>
  <div class="app-container">
    <div class="filter-container" style="text-align: right">
      <el-button class="filter-item" style="margin-left: 10px;" type="primary" icon="el-icon-edit" @click="handleCreate">
        Add
      </el-button>
    </div>
    <el-table
      :data="tableData"
      border
      stripe
      style="width: 100%"
    >
      <el-table-column
        v-for="item of addObj.addList"
        :key="item.id"
        :prop="item.discernName"
        :label="item.showName"
      />
    </el-table>
    <edit-button :add-obj="addObj" @editBtnValue="editBtnValue" />
  </div>
</template>

<script>
import { createArticle, fetchElmList } from '@/api/article'
import EditButton from '@/components/EditButton'
export default {
  name: 'ComplexTable',
  components: { EditButton },
  data() {
    return {
      menuId: 71,
      addObj: {
        type: 'create', // 点击添加按钮传Create  点击修改按钮传Update
        dialogFormVisible: false, // ture:弹出  false:取消
        addList: []
      },
      tempData: {},
      textMap: {
        update: 'Edit',
        create: 'Create'
      },
      tableData: [{
        id1: '哈利1',
        id2: '哈利2',
        id3: '哈利3',
        id4: '哈利4',
        id5: '哈利5',
        id6: '哈利6',
        id7: '哈利7',
        id8: '哈利8',
        id9: '哈利9',
        id10: '哈利10'
      }, {
        id1: '哈利1',
        id2: '哈利2',
        id3: '哈利3',
        id4: '哈利4',
        id5: '哈利5',
        id6: '哈利6',
        id7: '哈利7',
        id8: '哈利8',
        id9: '哈利9',
        id10: '哈利10'
      }, {
        id1: '哈利1',
        id2: '哈利2',
        id3: '哈利3',
        id4: '哈利4',
        id5: '哈利5',
        id6: '哈利6',
        id7: '哈利7',
        id8: '哈利8',
        id9: '哈利9',
        id10: '哈利10'
      }, {
        id1: '哈利1',
        id2: '哈利2',
        id3: '哈利3',
        id4: '哈利4',
        id5: '哈利5',
        id6: '哈利6',
        id7: '哈利7',
        id8: '哈利8',
        id9: '哈利9',
        id10: '哈利10'
      }]
    }
  },
  created() {
    this.fetchElmList()
  },
  methods: {
    editBtnValue(temp) {
      this.tempData = temp
      this.createData()
    },
    fetchElmList() {
      // 调用接口
      fetchElmList(this.menuId).then(response => {
        this.addObj.addList = response.data
      })
    },
    handleCreate() {
      this.addObj.dialogFormVisible = true
    },
    createData() {
      // 保存数据
      const array = this.tempData
      createArticle(array).then(() => {
        console.info(array)
        this.tableData.push(array)
        this.addObj.dialogFormVisible = false
        this.$notify({
          title: '保存成功',
          message: 'Created Successfully',
          type: 'success',
          duration: 2000
        })
      })
    }
  }
}
</script>
